package com.xmkj.bearbiz.member;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.common.BearBean.VipUserInfo;
import com.common.BearMethods.GetVipUserDatasMethods;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.xmkj.bearbiz.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.functions.Action1;

@SuppressLint("ValidFragment")
public class DressStyleFragment extends BaseMvpFragment {
    @BindView(R.id.et_job)
    EditText etHeight;
    @BindView(R.id.tv_shengcai)
    TextView tvShengcai;
    @BindView(R.id.iv_go)
    ImageView ivGo;
    @BindView(R.id.rl_shengcai)
    RelativeLayout rlShengcai;
    @BindView(R.id.tv_skin)
    TextView tvSkin;
    @BindView(R.id.iv_goo)
    ImageView ivGoo;
    @BindView(R.id.rl_skin)
    RelativeLayout rlSkin;
    @BindView(R.id.et_up_size)
    EditText etUpSize;
    @BindView(R.id.et_down_size)
    EditText etDownSize;
    @BindView(R.id.et_shoes_size)
    EditText etShoesSize;
    @BindView(R.id.et_pants_long)
    EditText etPantsLong;
    @BindView(R.id.et_love_color)
    EditText etLoveColor;
    @BindView(R.id.et_brank)
    EditText etBrank;
    @BindView(R.id.et_yaowei)
    EditText etYaowei;
    @BindView(R.id.et_xiongwei)
    EditText etXiongwei;

    Unbinder unbinder;
    private String deptcode ;
    private String vipcardid ;
    private VipUserInfo bean;
    private OptionsPickerView skinPicker;
    private OptionsPickerView fiturePicker;
    private SaveUserInfoBean submitBean = new SaveUserInfoBean();
    @SuppressLint("ValidFragment")
    public DressStyleFragment(String deptcode, String vipcardid) {
        this.deptcode =deptcode ;
        this.vipcardid = vipcardid ;
    }
    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dress_style;
    }

    @Override
    protected void onViewCreated(View view) {

    }

    @Override
    protected void doLogicFunc() {
        initFiturePiker();
        initSkinPicker();
        getVipUserData();
        attachClickListener(rlShengcai);
        attachClickListener(rlSkin);

        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.SAVE_USER_INFO) && (Boolean) rxKeyEvent.getValue()) {
//                   String baseInfo =   tvTip.getText().toString();
                    if(EmptyUtils.isEmpty(bean)){
                        submitBean.vipcardid = "";
                        submitBean.deptcode = "";
                    }else {
                        submitBean.vipcardid = bean.vipcardid;
                        submitBean.deptcode = bean.deptcode;
                    }
                    submitBean.height = getEditTextStr(etHeight);
                    submitBean.figure = getSubmitFigure(getTextViewString(tvShengcai));
                    submitBean.complexion = getSubmitSkin(getTextViewString(tvSkin));
                    submitBean.jacket_size = getEditTextStr(etUpSize);
                    submitBean.pants_size = getEditTextStr(etDownSize);
                    submitBean.shoes_size = getEditTextStr(etShoesSize);
                    submitBean.pants_length = getEditTextStr(etPantsLong);
                    submitBean.like_color = getEditTextStr(etLoveColor);
                    submitBean.like_brand = getEditTextStr(etBrank);
                    submitBean.waistline = getEditTextStr(etYaowei);
                    submitBean.circumference = getEditTextStr(etXiongwei);
                    RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.SAVE_FROM_DRESSSTYLE_INFO, submitBean));

                }
            }
        }));
    }

    private String getSubmitSkin(String skin) {
        if (EmptyUtils.isEmpty(skin)) {
            return "";
        } else if (skin.contains("偏白")) {
            return "1";
        } else if (skin.contains("标准")) {
            return "2";
        } else if (skin.contains("偏黑")) {
            return "3";
        }
        return skin;
    }

    private String getSubmitFigure(String figure) {
        if (EmptyUtils.isEmpty(figure)) {
            return "";
        } else if (figure.contains("X型")) {
            return "1";
        } else if (figure.contains("A型")) {
            return "2";
        } else if (figure.contains("H型")) {
            return "3";
        }else if (figure.contains("O型")) {
            return "4";
        }else if (figure.contains("Y型")) {
            return "5";
        }
        return figure;
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==rlShengcai.getId()){
            if(fiturePicker!=null){
                fiturePicker.show();
            }
        }else if(view.getId()==rlSkin.getId()){
            if(skinPicker!=null){
                skinPicker.show();
            }
        }
    }

    private void getVipUserData() {
        CommonSubscriber<VipUserInfo> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                bean = (VipUserInfo) o;

                etHeight.setText(bean.height);
                tvShengcai.setText(bean.figure);
                tvSkin.setText(bean.complexion);
                etUpSize.setText(bean.jacket_size);
                etDownSize.setText(bean.pants_size);
                etShoesSize.setText(bean.shoes_size);
                etPantsLong.setText(bean.pants_length);
                etLoveColor.setText(bean.like_color);
                etBrank.setText(bean.like_brand);
                etYaowei.setText(bean.waistline);
                etXiongwei.setText(bean.circumference);





            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        GetVipUserDatasMethods.getInstance().seeVipUser(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID),vipcardid,deptcode);
        rxManager.add(subscriber);
    }

    private void initSkinPicker() {
        final List<String> skinList = new ArrayList<>();
        skinList.add("偏白");
        skinList.add("标准");
        skinList.add("偏黑");

        skinPicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

//                shopCode = shopBeans.get(options1).lists.get(options2).clientcode;
//                tvCondition.setText(shopBeans.get(options1).lists.get(options2).clientname);
//                typecode = shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeCode;
//                tvGoodsSort.setText(one.get(options1) + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).TypeName + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeName);
            /*        tvProductYear.setText(years.get(options1));
                    year = years.get(options1);*/

//                tvTip.setText(bean.customer_identity_ids.get(options1));
                tvSkin.setText(skinList.get(options1));

            }
        }).setSubmitText(getString(R.string.insure)).
                setCancelText(getString(R.string.cacal)).
                build();
        skinPicker.setPicker(skinList);
    }

    private void initFiturePiker() {  // 身材
        final List<String> fitureList = new ArrayList<>();
        fitureList.add("X型");
        fitureList.add("A型");
        fitureList.add("H型");
        fitureList.add("O型");
        fitureList.add("Y型");

        fiturePicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {

//                shopCode = shopBeans.get(options1).lists.get(options2).clientcode;
//                tvCondition.setText(shopBeans.get(options1).lists.get(options2).clientname);
//                typecode = shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeCode;
//                tvGoodsSort.setText(one.get(options1) + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).TypeName + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeName);
            /*        tvProductYear.setText(years.get(options1));
                    year = years.get(options1);*/

//                tvTip.setText(bean.customer_identity_ids.get(options1));
                    tvShengcai.setText(fitureList.get(options1));


            }
        }).setSubmitText(getString(R.string.insure)).
                setCancelText(getString(R.string.cacal)).
                build();
        fiturePicker.setPicker(fitureList);
    }

    @Override
    public void bindKnife() {
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    protected void setStatusBar() {
        super.setStatusBar();
        m_statusBar.setVisibility(View.GONE);
    }
}
